package com.xinqi.modules.chat.chat.message.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.constant.ChatRemindConstants;
import com.xinqi.common.core.enums.ChatRemindTypeEnum;
import com.xinqi.common.core.utils.PlaceholderUtils;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.chat.chat.message.convert.ChatRemindMessageConvert;
import com.xinqi.modules.chat.chat.message.domain.ChatRemindMessageEntity;
import com.xinqi.modules.chat.chat.message.dto.req.ChatRemindMessageCreateDTO;
import com.xinqi.modules.chat.chat.message.manager.ChatRemindMessageManager;
import com.xinqi.modules.chat.chat.message.service.ChatRemindMessageService;
import com.xinqi.modules.chat.chat.official.dto.req.ChatRemindContentBatchCreateDTO;
import com.xinqi.modules.chat.chat.official.dto.req.ChatRemindContentCreateDTO;
import com.xinqi.modules.chat.chat.official.service.impl.ChatRemindMQService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 聊天消息提醒表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/07/05
 */
@Service("chatRemindMessageService")
@RequiredArgsConstructor
public class ChatRemindMessageServiceImpl implements ChatRemindMessageService {
    private final ChatRemindMessageConvert chatRemindMessageConvert;

    private final ChatRemindMessageManager chatRemindMessageManager;

    private final ChatRemindMQService chatRemindMQService;

    /**
     * 添加聊天消息提醒
     *
     * @param dto
     * @param user
     * @return
     */
    @Override
    public Long addRemindMessage(ChatRemindMessageCreateDTO dto, Account user) {
        ChatRemindMessageEntity entity = chatRemindMessageConvert.create(dto);
        entity.setRemindUser(user.getId());
        entity.setStatus(YesNoNumberEnum.NO.getCode());
        chatRemindMessageManager.save(entity);
        return entity.getId();
    }

    /**
     * 消息提醒-定时提醒
     */
    @Override
    public void sendRemindMessage() {
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //获取所有未提醒过的的消息
        LambdaQueryWrapper<ChatRemindMessageEntity> wrapper = Queries.lambda();
        wrapper.eq(ChatRemindMessageEntity::getStatus, YesNoNumberEnum.NO.getCode())
            .le(ChatRemindMessageEntity::getRemindTime, now);
        List<ChatRemindMessageEntity> list = chatRemindMessageManager.list(wrapper);
        //发送提醒的ID
        List<Long> ids = new ArrayList<>();
        //新奇提醒
        List<ChatRemindContentCreateDTO> remindList = new ArrayList<>();
        list.stream().forEach(entity -> {
            String title = "";
            if (Objects.equals("RC:TxtMsg", entity.getType())) {
                title = entity.getText();
            } else if (Objects.equals("RC:HQVCMsg", entity.getType())) {
                title = "语音";
            } else if (Objects.equals("RC:LBSMsg", entity.getType())) {
                title = "位置";
            } else {
                title = entity.getFileName();
            }
            //新奇提醒机器人 -- 消息提醒
            Map<String, Object> params = new HashMap<>();
            params.put("title", title);
            String content = PlaceholderUtils.replaceWithMap(ChatRemindConstants.REMIND_MESSAGE_REMINDER_TEMPLATE, params);
            remindList.add(new ChatRemindContentCreateDTO(entity.getRemindUser(), ChatRemindTypeEnum.MESSAGE_REMINDER, content, JSON.toJSONString(chatRemindMessageConvert.convert(entity))));
            ids.add(entity.getId());
        });
        if (CollectionUtils.isNotEmpty(remindList)) {
            chatRemindMQService.batchSendRemind(new ChatRemindContentBatchCreateDTO(remindList));
        }
        if (CollectionUtils.isNotEmpty(ids)) {
            LambdaUpdateWrapper<ChatRemindMessageEntity> update = new LambdaUpdateWrapper<>();
            update.set(ChatRemindMessageEntity::getStatus, YesNoNumberEnum.YES.getCode())
                .in(ChatRemindMessageEntity::getId, ids);
            chatRemindMessageManager.update(update);
        }
    }
}
